library(tidyr)
library(dplyr)
library(lubridate)
library(zoo)
library(ggplot2)
library(limma)
library(ggpubr)
library(grid)
library(plotly)

#Data Files and prep work
source("../../lib/DataProccess.R")
source("../../lib/NormFuncs.R")
source("../../lib/OutlierDetectionFuncs.R")
source("../../lib/DataPathName.R")
source("InterceptorPlots.R")
BaseDir <- params$BaseDir#get the root of the directory where the data is stored

“Files Used:”

COVID-19_WastewaterAnalysis/data/processed/LIMSWasteData_02-09-22.csv

GGPlotlyWithScaling <- function(GGPlotObject, FactorSize, Display = c("Date","N1","N2")){
  AlphaScalling <- scale_alpha_manual(values = seq(.3,1, length.out = FactorSize))
  SizeScalling <- scale_size_manual(values = seq(.7,2, length.out = FactorSize))
  #ShapeSCalling <- scale_shape_discrete(values = c(21,23))
  PlotlyObject <- GGPlotObject+
    AlphaScalling+
    SizeScalling
  return(ggplotly(PlotlyObject, tooltip = Display))
}


ThresholdingLabel <- function(X,ThresholdC){
  A = 0
  for (i in 1:length(ThresholdC)){
    A = A + (X<=ThresholdC[i])

  }
  return(factor(A))
}

ThresholdGenDF <- function(DF, SysUsed ,Thresholds){
  ThresholdFlagDF <- DF%>%
    filter(!is.na(!!sym(SysUsed)))%>%
    mutate(ThresholdOutlier= ThresholdingLabel(!!sym(SysUsed),Thresholds))%>%
    arrange(Date,Site)
  
  return(ThresholdFlagDF)
}

ThresholdingPloting <- function(DF, SysUsed="", ThreshLevel = "" , isFacet = FALSE){
  if(isFacet){
    ColorScheme = ThreshLevel
  }else{
    ColorScheme = "Site"
  }#strafy
  GPlot <- DF%>%
    filter(!is.na(!!sym(ThreshLevel)))%>%
    ggplot(aes(x = Date))+
    theme(panel.spacing.y = unit(2, "lines"))+
    geom_point(aes(y = N1, fill = !!sym(ColorScheme), shape = "N1", info = !!sym(SysUsed),
                   alpha = !!sym(ThreshLevel), size = !!sym(ThreshLevel)),
               color = "Black",pch=21, stroke = .15)+
    geom_point(aes(y = N2, fill = !!sym(ColorScheme), shape = "N2", info = !!sym(SysUsed),
                   alpha = !!sym(ThreshLevel), size = !!sym(ThreshLevel)),
               color = "Black",pch=24, stroke = .15)
  if(isFacet){
    GPlot = GPlot+
      facet_wrap(~Site, ncol = 1, scales = "free")
  }
  return(GPlot)
}
DoRanking <- function(DF,IsGrouped,isFiltered){
  UsedDF <- DF%>%
    filter(!is.na(N1),!is.na(N2))
  if(IsGrouped){
    UsedDF <- UsedDF%>%
      group_by(Site)
  }
  if(isFiltered){
    UsedDF <- UsedDF%>%
      filter(Date<mdy("10/31/2021"))
  }
  RetDF <- UsedDF%>%
    arrange(Date)%>%
    mutate(N1 = ifelse(!is.na(N1),N1,0))%>%
    mutate(N2 = ifelse(!is.na(N2),N2,0))%>%
    mutate(N1RankLeft = rank(desc(N1 - lag(N1))),
         N1RankRight = rank(desc(N1 - lead(N1))),
         N2RankLeft = rank(desc(N2 - lag(N2))),
         N2RankRight = rank(desc(N2 - lead(N2))))%>%
    select(Date,Site,N1RankLeft,N1RankRight,N2RankLeft,N2RankRight,N1,N2)%>%
    mutate(MaxN1Rank = pmax(N1RankLeft,N1RankRight,N2RankLeft,N2RankRight))%>%
    select(Date,Site,MaxN1Rank,N1,N2)
  return(RetDF)
}

JoinDFFromList <- function(DFList, ByOptions, Names,CombVec){
  FullDF <- full_join(DFList[[1]],DFList[[2]], by = ByOptions)
  for (i in 3:length(DFList)){
    FullDF <- full_join(FullDF,DFList[[i]], by = ByOptions)
  }
  for(i in 1:length(DFList)){
    if(i %% 2 == 1){
      Prep = paste(CombVec, paste(rep(".x",(i+1)%/%2), collapse = ""), sep = "")
    }else{
      Prep = paste(CombVec, paste(rep(".y",(i+1)%/%2), collapse = ""), sep = "")
    }
    FullDF <- FullDF%>%
      rename(!!Names[[i]] := !!sym(Prep))
  }
  return(FullDF)
}
RankingDFUngroupedEarlyDay <- DoRanking(LIMSFullDF,FALSE,TRUE)
RankingDFgroupedEarlyDay <- DoRanking(LIMSFullDF,TRUE,TRUE)
RankingDFUngrouped <- DoRanking(LIMSFullDF,FALSE,FALSE)
RankingDFgrouped <- DoRanking(LIMSFullDF,TRUE,FALSE)

ToMergeDFs <- list(RankingDFUngroupedEarlyDay, RankingDFgroupedEarlyDay, RankingDFUngrouped, RankingDFgrouped)
RenameLists <- list("EarlyFull", "EarlySite", "AllFull", "AllSite")

MergedDF <- JoinDFFromList(
  ToMergeDFs, ByOptions = c("Date","Site","N1","N2"), Names = RenameLists, CombVec = "MaxN1Rank")
Method = "EarlySite"
#"EarlyFull", "EarlySite", "AllFull", "AllSite"

Thresh <- c(4)
UsedDF <- ThresholdGenDF(MergedDF, Method, Thresh)%>%
  rename(Threshold = ThresholdOutlier)%>%
  mutate(Threshold = ifelse((Threshold==1),"Strict","Inliers"))

PlotedGraphic <- ThresholdingPloting(UsedDF, SysUsed = "EarlySite",ThreshLevel = "Threshold")+
  #SiteColorScheme()+
  YLabN1_N2()

GGPlotlyWithScaling(PlotedGraphic, length(Thresh)+1)
Thresh <- c(4, 11)
UsedDF <- ThresholdGenDF(MergedDF, Method, Thresh)%>%
  rename(Threshold = ThresholdOutlier)%>%
  mutate(Threshold = ifelse((Threshold)==1,"Permissive",
                                   ifelse((Threshold==2),"Strict","Inliers")))
UsedDF%>%
  dplyr::filter(Threshold!="Inliers")%>%
  select(Date,Site, N1, N2, Threshold)#%>%
  #write.csv("RmdOutput/SendOutliersShortList.csv")

PlotedGraphic <- ThresholdingPloting(UsedDF, SysUsed = "EarlySite",ThreshLevel = "Threshold", isFacet =TRUE)+
  YLabN1_N2()

GGPlotlyWithScaling(PlotedGraphic, length(Thresh)+1)